import { createSlice } from '@reduxjs/toolkit'
import * as api from 'src/api'

type User = {
  pid: number
  userInfo: {
    uid?: number
    [key: string]: any
  }
}

const initialState: User = {
  pid: 0,
  userInfo: {},
}

export const counterSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    setPid: (state, { payload }) => {
      state.pid = payload
    },
    setUserInfo: (state, { payload }) => {
      state.userInfo = payload
    },
  },
})

export const selectShowAuth = (state) => state.user.showAuth

export const { setPid, setUserInfo } = counterSlice.actions

export const getUserInfo = () => async (dispatch) => {
  const res = await api.getUserInfo()
  dispatch(setUserInfo(res))
  return res
}

export default counterSlice.reducer
